package com.sg.business.permission.ui.column;

import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;

import com.sg.business.permission.model.PermissionAction;
import com.sg.business.permission.model.ValidationField;
import com.sg.widgets.registry.config.Option;

public class ValidationFieldOptionColumn {

	private boolean isEditable;

	private ValidationField field;

	public ValidationFieldOptionColumn(ValidationField field) {
		this.field = field;
	}

	public TableViewerColumn create(final TableViewer viewer) {
		final Option option = field.getOption();

		TableViewerColumn column = new TableViewerColumn(viewer, SWT.LEFT);
		column.getColumn().setMoveable(true);
		column.getColumn().setWidth(80);
		column.getColumn().setText(field.getFieldName());

		column.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Object value = ((PermissionAction) element)
						.getValidationValue(field.getFieldCode());
				if(value instanceof List<?>){
					String result = "";
					for(int i=0;i<((List<?>) value).size();i++){
						if(i!=0){
							result+=",";
						}
						Option[] children = option.getChildren();
						for (int j = 0; j < children.length; j++) {
							if (children[j].getValue().equals(((List<?>) value).get(i))) {
								result+=children[j].getLabel();
								break;
							}
						}
					}
					return result;
					
				}
				return super.getText(value);
			}

		});

		createSelectorCell(viewer, column, option);
		return column;
	}

	private void createSelectorCell(final TableViewer viewer,
			TableViewerColumn column, final Option option) {
		final CellEditor cellEditor = new ValidationFieldCellEditor(
				viewer.getTable(), option.getChildren());
		column.setEditingSupport(new EditingSupport(viewer) {

			protected void setValue(Object element, Object value) {
				ValidationFieldOptionColumn.this.setValue(viewer,
						(PermissionAction) element, field.getFieldCode(), value);
			}

			@Override
			protected Object getValue(Object element) {
				return ValidationFieldOptionColumn.this.getValue(
						(PermissionAction) element, field.getFieldCode());
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				return cellEditor;
			}

			@Override
			protected boolean canEdit(Object element) {
				return ValidationFieldOptionColumn.this.canEdit(viewer,
						(PermissionAction) element);
			}
		});
	}

	protected boolean canEdit(TableViewer viewer, PermissionAction action) {
		return isEditable() && action.getActionDefinition().isEditable();
	}

//	private void createTextCell(final TableViewer viewer,
//			TableViewerColumn column) {
//		final TextCellEditor cellEditor = new TextCellEditor(viewer.getTable());
//		column.setEditingSupport(new EditingSupport(viewer) {
//
//			@Override
//			protected void setValue(Object element, Object value) {
//				ValidationFieldOptionColumn.this.setValue(viewer,
//						(PermissionAction) element, field.getFieldCode(), value);
//			}
//
//			@Override
//			protected Object getValue(Object element) {
//				return ValidationFieldOptionColumn.this.getValue(
//						(PermissionAction) element, field.getFieldCode());
//			}
//
//			@Override
//			protected CellEditor getCellEditor(Object element) {
//				return cellEditor;
//			}
//
//			@Override
//			protected boolean canEdit(Object element) {
//				return ValidationFieldOptionColumn.this.canEdit(viewer,
//						(PermissionAction) element);
//			}
//		});
//	}

	protected void setValue(TableViewer viewer, PermissionAction action,
			String key, Object value) {
		action.setValidationValue(key, value);
		viewer.update(action, null);
	}

	protected Object getValue(PermissionAction action, String key) {
		Object value = action.getValidationValue(key);
		return value;
	}

	public boolean isEditable() {
		return isEditable;
	}

	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
	}

}
